package com.mooc.lib_find.ui.find_ist

import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.mooc.lib_common.bus.LiveDataBus
import com.mooc.lib_find.adapter.FindListAdapter
import com.mooc.lib_find.model.FindFollowEntity
import com.mooc.lib_find.model.FindListEntity
import com.mooc.lib_network.entity.NetResult
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class FindViewModel : ViewModel(),OnRefreshLoadMoreListener,OnItemChildClickListener{
    var repo : FindRepo
    init {
        repo = FindRepo()
    }

    var userId = 1578644405
    var followOffset = 0
    var followPageCount = 0
    var recommandOffset = 0
    var recommandPageCount = 0
    val refreshFollowBus = LiveDataBus<FindListEntity>()
    val loadFollowBus = LiveDataBus<FindListEntity>()
    val refreshRecommandBus = LiveDataBus<FindListEntity>()
    val loadRecommandBus = LiveDataBus<FindListEntity>()
    val followBus : LiveDataBus<FindFollowEntity> = LiveDataBus()
    //关注列表下拉刷新
    fun refreshFollow(){
        followOffset = 0
        followPageCount = 50
        viewModelScope.launch(Dispatchers.IO){
            val result = repo.find(userId,"onlyFollow",1,followPageCount,followOffset)
            if (result is NetResult.Success){
                if (result.data ==null)
                    return@launch
                if (result.data.data == null)
                    return@launch
                if (result.data.data!!.size == 0)
                    return@launch
                refreshFollowBus.postValue(result.data)
            }
        }
    }
    //推荐列表下拉刷新
    fun refreshRecommand(){
        recommandOffset = 0
        recommandPageCount = 50
        viewModelScope.launch(Dispatchers.IO){
            val result = repo.find(userId,"excludeFollow",2,followPageCount,followOffset)
            if (result is NetResult.Success){
                if (result.data ==null)
                    return@launch
                if (result.data.data == null)
                    return@launch
                if (result.data.data!!.size == 0)
                    return@launch
                refreshRecommandBus.postValue(result.data)
            }
        }
    }
    //关注列表上拉加载
    fun loadFollow(){
        followOffset = 0
        followPageCount += 50
        viewModelScope.launch(Dispatchers.IO){
            val result = repo.find(userId,"onlyFollow",1,followPageCount,followOffset)
            if (result is NetResult.Success){
                if (result.data ==null)
                    return@launch
                if (result.data.data == null)
                    return@launch
                if (result.data.data!!.size == 0)
                    return@launch
                loadFollowBus.postValue(result.data)
            }
        }
    }
    //推荐列表上拉加载
    fun loadRecommand(){
        recommandOffset = 0
        recommandPageCount += 50
        viewModelScope.launch(Dispatchers.IO){
            val result = repo.find(userId,"excludeFollow",2,followPageCount,followOffset)
            if (result is NetResult.Success){
                if (result.data ==null)
                    return@launch
                if (result.data.data == null)
                    return@launch
                if (result.data.data!!.size == 0)
                    return@launch
                loadRecommandBus.postValue(result.data)
            }
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        refreshLayout.finishRefresh()
        val smart = refreshLayout as SmartRefreshLayout
        val tag = smart.tag
        if (tag.equals("onlyFollow")){
            refreshFollow()
        }else
            refreshRecommand()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        refreshLayout.finishLoadMore()
        val smart = refreshLayout as SmartRefreshLayout
        val tag = smart.tag
        if (tag.equals("onlyFollow")){
            loadFollow()
        }else
            loadRecommand()
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val recommandAdapter : FindListAdapter = adapter as FindListAdapter
        val entity = recommandAdapter.getItem(position)
        viewModelScope.launch(Dispatchers.IO){
            val result = repo.follow(userId,entity.tagId)
            if (result is NetResult.Success){
                if (result.data.data!!.hasFollow){
                    entity.hasFollow = true
                    //关注成功
                    result.data.data!!.position = position
                    result.data.data!!.entity = entity
                    followBus.postValue(result.data.data!!)
                }
            }
        }
    }


}